//
//  HMHomeReweetView.swift
//  Weibo
//
//  Created by itteacher on 2017/7/26.
//  Copyright © 2017年 itteacher. All rights reserved.
//

import UIKit
import SnapKit

//  转发微博视图
class HMHomeReweetView: UIView {
    
    //转发微博底部约束
    var reweetViewBottomConstraint: Constraint?
    
    var weiBoViewModel: TTWeiBoViewModel?{
        didSet{
            
            contentLabel.text = weiBoViewModel?.retweetedContent
            
            //卸载约束
            reweetViewBottomConstraint?.deactivate()
            
//            print("22222")
//            print(weiBoViewModel?.weiBoModel?.pic_urls?.count ?? 0)
            
            if weiBoViewModel?.weiBoModel?.retweeted_status?.pic_urls != nil && weiBoViewModel?.weiBoModel?.retweeted_status?.pic_urls?.count != 0{
                
                picColView.isHidden = false
                
                self.snp.makeConstraints({ (make) in
                    reweetViewBottomConstraint = make.bottom.equalTo(picColView).offset(HomeCellMargin).constraint
                })
                //有图片url,就为collectionView传递图片数组
                picColView.picArray = weiBoViewModel?.weiBoModel?.retweeted_status?.pic_urls
                
            }else{
               
                //collectionView不显示
                picColView.isHidden = true
                
                //重新建立底部约束并重新记录
                self.snp.makeConstraints({ (make) in
                    reweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })

            }

        }
    }

    //  MARK: --懒加载控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: 13, textColor: UIColor.lightGray)
        //  多行显示
        label.numberOfLines = 0
        return label
        
    }()
    
    // 懒加载图片视图
    private lazy var picColView: TTPicCollectionView = {
       
        let picView = TTPicCollectionView()
        
        picView.backgroundColor = UIColor.darkGray
        
        return picView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //  添加控件设置约束
    private func setupUI() {
//        backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        backgroundColor = UIColor.blue
        addSubview(contentLabel)
        addSubview(picColView)
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        picColView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
//            make.leading.equalTo(contentLabel)
//            make.trailing.equalTo(self).offset(-HomeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
//            make.bottom.equalTo(self).offset(-HomeCellMargin)
        }
        
        //  关键约束
        self.snp.makeConstraints { (make) in
            //记录约束
            reweetViewBottomConstraint = make.bottom.equalTo(picColView).offset(HomeCellMargin).constraint
        }
    
    }
}
